package com.csi.controller.employee;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.User;
import com.csi.pojo.Result;
import com.csi.domain.Employee;
import com.csi.service.EmployeeService;
import com.csi.service.UserService;
import com.csi.service.impl.EmployeeServiceImpl;
import com.csi.service.impl.UserServiceImpl;
import com.csi.utils.DateStringFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/employee/addController")
public class AddController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        // 获取请求参数
        String name = req.getParameter("name");
        String englishName = req.getParameter("englishName");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String gender = req.getParameter("gender");
        String bornDate = req.getParameter("bornDate");

        // 判断姓名是否为空或者不是中文
        if (name == null || !name.matches("^[\\u4e00-\\u9fa5]+$")) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("姓名不能为空且必须为中文");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断英文名是否为空或者包含数字或中文
        if (englishName.trim().equals("") || englishName.matches(".*[\\d\\u4e00-\\u9fa5]+.*")) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("英文名格式不正确");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }


        // 判断邮箱格式是否正确
        if (!email.matches("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("邮箱格式不正确");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断联系电话格式是否正确
        if (!phone.matches("^1[3456789]\\d{9}$")) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("联系电话格式不正确");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断生日格式是否正确
        if (!bornDate.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("生日格式不正确");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 判断生日是否小于当前日期
        Date currentDate = new Date();

        Date selectedDate = DateStringFormat.string2Date(bornDate);
        if (selectedDate.compareTo(currentDate) > 0) {
            Result<String> result = new Result<>();
            result.setCode(400);
            result.setMsg("生日不能大于当前日期");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        Date date = DateStringFormat.string2Date(bornDate);

        // 判断手机号是否已存在
        UserService userService = new UserServiceImpl();
        try {
            List<User> userList = userService.findAll();
            for (User u : userList) {
                if (u.getPhone().equals(phone)) {
                    Result<String> result = new Result<>();
                    result.setCode(401);
                    result.setMsg("该手机号已被注册");
                    out.print(JSON.toJSONString(result));
                    out.flush();
                    out.close();
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Result<String> result = new Result<>();
            result.setCode(500);
            result.setMsg("查询手机号失败");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }

        // 创建User对象
        User user = new User();
        user.setUserName(name);
        user.setLoginAccount(phone); // 手机号作为账号
        user.setEnglishName(englishName);
        user.setEmail(email);
        user.setPhone(phone);
        user.setGender(gender);
        user.setBorndate(date);

        // 创建Employee对象
        Employee employee = new Employee();
        employee.setOnboardingDate(new Date());

        // 调用EmployeeService的方法添加员工
        EmployeeService employeeService = new EmployeeServiceImpl();
        try {
            employeeService.add(user, employee);
            Result<String> result = new Result<>();
            result.setCode(200);
            result.setMsg("success");
            out.print(JSON.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
            Result<String> result = new Result<>();
            result.setCode(500);
            result.setMsg("fail");
            out.print(JSON.toJSONString(result));
        } finally {
            out.flush();
            out.close();
        }

    }
}